package com.atguigu.filter;

import com.atguigu.utils.jdbcUtils;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author 14587
 * @date 2020/10/25 10:34
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request,response);
            jdbcUtils.commitAndClose();
        } catch (Exception e) {
            jdbcUtils.rollbackAndClose();
            e.printStackTrace();
            //这里因为我们想要tomcat服务器处理错误友好页面，所以还需要往外面抛异常
            throw new RuntimeException(e);
        }
    }

    @Override
    public void destroy() {

    }
}
